package hydrap2p.search;

import hydrap2p.ServiceLocation;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class SearcherThrift implements SearcherIface {

	private SearchProvider.Client connection;
	private ServiceLocation location;
	private String name;
	
	public SearcherThrift(ServiceLocation location) throws TException
	{
		if (location==null)
			throw new TException("Invalid service location");
		
		this.location=location;

		TTransport transport = new TSocket(location.address,location.port);
		transport.open();
		connection=new SearchProvider.Client(new TBinaryProtocol(transport));
		
		if (!connection.supportedInterfaces().contains("SearchProvider"))
			throw new TException("Module does not implement SearchProvider");
		
		name=connection.getName();
	}
	
	public ServiceLocation getLocation()
	{
		return location;
	}
	
	@Override
	public String getName() {
		return name;
	}

	@Override
	public void startSearch(int searchid, SearchQuery query) throws Exception {
		connection.startSearch(searchid, query);
	}

	@Override
	public void stopSearch(int searchid) throws Exception {
		connection.stopSearch(searchid);
	}

}
